mui.init({
	pullRefresh: {
		container: '#pullrefresh',
		up: {
			contentrefresh: '正在加载...',
			callback: pullupRefresh
		}
	}
});

Vue.use(VueLazyload, {
	preLoad: 1.3,
	error: '../../images/noimg.jpg',
	loading: '../../images/noimg.jpg'
});
mui('#pullrefresh').scroll();

//var nativeWebview, imm, InputMethodManager;
//var initNativeObjects = function() {
//	if(mui.os.android) {
//		var main = plus.android.runtimeMainActivity();
//		var Context = plus.android.importClass("android.content.Context");
//		InputMethodManager = plus.android.importClass("android.view.inputmethod.InputMethodManager");
//		imm = main.getSystemService(Context.INPUT_METHOD_SERVICE);
//	} else {
//		nativeWebview = plus.webview.currentWebview().nativeInstanceObject();
//	}
//};
//var showSoftInput = function() {
//	if(mui.os.android) {
//		imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
//	} else {
//		nativeWebview.plusCallMethod({
//			"setKeyboardDisplayRequiresUserAction": false
//		});
//	}
//	setTimeout(function() {
//		var inputElem = document.getElementById('seachInputs');
//		inputElem.focus();
//	}, 200);
//};
//mui.plusReady(function() {
//	initNativeObjects();
//	showSoftInput();
//});
var seach = new Vue({
	el: '#seach',
	data: {
		imgCommonUrl: common.imageUrl,
		show: true,
		nodata: false,
		shopSeachHistory: JSON.parse(localStorage.getItem("shopSeachHistory")) || [],
		val: '',
		//排序
		sortArr: [{
			text: '全部',
			val: 'recDate'
		}, {
			text: '销量',
			val: 'buys'
		}, {
			text: '价格排序',
			val: 'cash'
		}, {
			text: '人气',
			val: 'comments'
		}],
		sortNum: 0,
		orders: 'asc',
		pageNum: 1,
		list: [],
		shopId: '',
		backdrop: false
	},
	methods: {
		seachbuttom: function() {
			$(".seach_val").blur()
			if(common.replace(this.val) == '' || !this.show) {
				return
			}
			for(var i = 0; i < this.shopSeachHistory.length; i++) {
				if(this.shopSeachHistory[i] == this.val) {
					this.shopSeachHistory.splice(i, 1)
				}
			}
			this.shopSeachHistory.unshift(this.val)
			localStorage.setItem('shopSeachHistory', JSON.stringify(this.shopSeachHistory))
			this.show = false
			mui('#pullrefresh').pullRefresh().refresh(true);
			seach.list = []
			seach.pageNum = 1
			seach.orders = 'desc'
			seach.sortNum = 0
			mui('#pullrefresh').pullRefresh().pullupLoading();
			mui('#pullrefresh').pullRefresh().scrollTo(0, 0, 0.1)
		},
		seachfocus: function() {
			this.val = ''
			this.show = true
			mui('#pullrefresh').pullRefresh().scrollTo(0, 0, 0.1)
			$(".noshopp").hide()
		},
		removehistory: function() {
			localStorage.removeItem("shopSeachHistory")
			this.shopSeachHistory = JSON.parse(localStorage.getItem("shopSeachHistory")) || []
			mui('#pullrefresh').pullRefresh().scrollTo(0, 0, 0.1)
		},
		removeThis: function(data) {
			for(var i = 0; i < this.shopSeachHistory.length; i++) {
				if(this.shopSeachHistory[i] == data) {
					this.shopSeachHistory.splice(i, 1)
				}
			}
			localStorage.setItem('shopSeachHistory', JSON.stringify(this.shopSeachHistory))
		},
		clsSort: function(index) {
			if(seach.sortNum == index) {
				seach.orders = seach.orders == 'desc' ? 'asc' : 'desc'
			} else {
				seach.sortNum = index
				seach.orders = 'asc'
			}
			mui('#pullrefresh').pullRefresh().refresh(true);
			seach.list = []
			seach.pageNum = 1
			mui('#pullrefresh').pullRefresh().pullupLoading();
			mui('#pullrefresh').pullRefresh().scrollTo(0, 0, 0.1)
		},
		historyTap: function(data) {
			this.val = data
			for(var i = 0; i < this.shopSeachHistory.length; i++) {
				if(this.shopSeachHistory[i] == this.val) {
					this.shopSeachHistory.splice(i, 1)
				}
			}

			this.shopSeachHistory.unshift(this.val)
			localStorage.setItem('shopSeachHistory', JSON.stringify(this.shopSeachHistory))
			this.show = false
			mui('#pullrefresh').pullRefresh().refresh(true);
			seach.list = []
			seach.pageNum = 1
			seach.orders = 'desc'
			seach.sortNum = 0
			mui('#pullrefresh').pullRefresh().pullupLoading();
			mui('#pullrefresh').pullRefresh().scrollTo(0, 0, 0.1)
		},
		openDetails: function(data) {
			common.open('../details/details.html', 'details.html', data.prodId)
		},
		addShopCar: function(data) {
			mui.plusReady(function() {
				seach.backdrop = true
				var w = plus.webview.create('../mask/clsaddcart.html', '../mask/clsaddcart.html', {
					top: '40%',
					bottom: '0'
				}, {
					preload: data.prodId
				});
				w.show('slide-in-bottom'); // 显示窗口
			})
		}
	},
	watch: {
		show: function(olddata, newdata) {
			if(newdata && seach.list.length > 0) {
				$('.mui-pull-bottom-pocket').css('display', 'block !important')
			} else {
				$('.mui-pull-bottom-pocket').css('display', 'none !important')
			}
		}
	}
})

mui.plusReady(function() {
	var self = plus.webview.currentWebview();
	seach.shopId = self.shopId
	console.log(self.shopId)
})

function pullupRefresh() {
	$(".noshopp").hide()
	$('.mui-pull-bottom-pocket').css('display', 'block !important')
	if(seach.show) {
		mui('#pullrefresh').pullRefresh().endPullupToRefresh()
		return
	}
	console.log('/shop/storeProdctList/' + seach.shopId)
	var temp = seach.sortNum == 0 ? '' : seach.sortArr[seach.sortNum].val + ',' + seach.orders
	var datas = {
		curPageNO: seach.pageNum++,
		order: temp,
		keyword: seach.val
	}
	common.ajax('/shop/storeProdctList/' + seach.shopId, datas, function(data) {
		if(data.code == 1) {
			seach.list = seach.list.concat(data.data.list)
			document.getElementById('seachInputs').blur()
			if(seach.pageNum > data.data.pageCount) {
				mui('#pullrefresh').pullRefresh().endPullupToRefresh(true);
			} else {
				mui('#pullrefresh').pullRefresh().endPullupToRefresh();
			}

			if(seach.list.length <= 0) {
				$(".noshopp").show()
				$('.mui-pull-bottom-pocket').css('display', 'none !important')
			} else {
				$(".noshopp").hide()
				$('.mui-pull-bottom-pocket').css('display', 'block !important')
			}

		} else {
			if(data.resultMsg) {
				mui.toast(data.resultMsg)
			}
			mui('#pullrefresh').pullRefresh().endPullupToRefresh()
		}
	}, true, 'get')
}
window.addEventListener('hideMask', function() {
	seach.backdrop = false
});
var close = mui.back
mui.back = function() {
	if(seach.backdrop) {
		seach.backdrop = false
		plus.webview.close('../mask/clsaddcart.html');
	} else {
		close()
	}
}